package com.vst.orm.util;

import java.util.Date;
import java.util.Map;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 对象json化时对时间类型字段格式化
 * @author 江荣盛
 */

public class JsonValueProcessImpl implements JsonValueProcessor {
	private String formate;
	private Map dateFormat;
	public JsonValueProcessImpl() {
		this.formate = "yyyy-MM-dd";
	}

	public JsonValueProcessImpl(String formate) {
		this.formate = formate;
	}
	
	public JsonValueProcessImpl(Map dateFormat) {
		this.dateFormat = dateFormat;
	}
	
	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return null;
	}

	public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
		if(value instanceof Date){
			String format = "yyyy-MM-dd HH:mm:ss";
        	if(dateFormat != null && !dateFormat.isEmpty())
        	{
        		format = dateFormat.get(key) != null ? dateFormat.get(key).toString() : format;
        		
			}
			String date = DateUtil.dateToString((Date)value, format);
			return date;
		}
		return null;
	}

	public String getFormate() {
		return formate;
	}

	public void setFormate(String formate) {
		this.formate = formate;
	}

}


